#include "MoveObject.h"

CMoveObject::CMoveObject()
:m_Speed(0), m_Direction(0)
{
	SetBlock(true);
}

CMoveObject::CMoveObject(float speed, int dir)
:m_Speed(speed), m_Direction(dir)
{
	SetBlock(true);
}

CMoveObject::~CMoveObject(void)
{
}

bool CMoveObject::OnMove(float dt)
{
	float x = GetX();
	float y = GetY();
	switch(m_Direction)
	{
	case DIRECTION_UP:
		y -= m_Speed*dt;
		break;
	case DIRECTION_RIGHT:
		x += m_Speed*dt;
		break;
	case DIRECTION_DOWN:
		y += m_Speed*dt;
		break;
	case DIRECTION_LEFT:
		x -= m_Speed*dt;
		break;
	default:
		break;
	}
	MoveRect(x,y);
	return true;
}

bool CMoveObject::OnMove(float dt, int dir)
{
	if(dir < DIRECTION_UP || dir > DIRECTION_LEFT)
		return false;

	m_Direction = dir;
	return OnMove(dt);
}

bool CMoveObject::OnImpact(hgeRect* rt)
{
	return m_Rect->Intersect(rt);
}

bool CMoveObject::OnImpact(float l, float t, float w, float h)
{
	hgeRect* rt = new hgeRect(l,t,l+w,t+h);
	bool rst = OnImpact(rt);
	delete rt;
	rt = NULL;
	return rst;
}

void CMoveObject::SetMovement(float speed, int direction)
{
	m_Speed = speed;
	m_Direction = direction;
}

bool CMoveObject::OnOutside(float line, int direction)
{
	switch(direction)
	{
	case DIRECTION_UP:
		{
			if(GetY() < line)
				return true;
			break;
		}
	case DIRECTION_RIGHT:
		{
			if(m_Rect->x2 > line)
				return true;
			break;
		}
	case DIRECTION_DOWN:
		{
			if(m_Rect->y2 > line)
				return true;
			break;
		}
	case DIRECTION_LEFT:
		{
			if(GetX() < line)
				return true;
			break;
		}
	default:
		break;
	}
	return false;
}